<%@page import="cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem"%>
<%@page import="cn.edu.nju.ws.sview.rdf.URIResource"%>
<%@page import="cn.edu.nju.ws.sview.wyge.relations.adataset.comment.DatasetComment"%>
<%@page import="cn.edu.nju.ws.sview.wyge.relations.adataset.CustomDataset"%>
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.net.URLDecoder,cn.edu.nju.ws.sview.Config,cn.edu.nju.ws.sview.views.Comment,cn.edu.nju.ws.sview.views.Image,cn.edu.nju.ws.sview.views.Group,cn.edu.nju.ws.sview.util.URIUtil,java.net.URLEncoder,java.util.Enumeration,java.util.ArrayList,cn.edu.nju.ws.sview.util.URIUtil,cn.edu.nju.ws.sview.views.Entity,cn.edu.nju.ws.sview.views.data.EntityManagement,cn.edu.nju.ws.sview.views.data.EntityManagement.URIMember,cn.edu.nju.ws.sview.cache.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Dataset Edit</title>
<link rel="icon" href="/sview/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/sview/css/layout.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/smoothness/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/relations/dataset_edit.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/easyui.css" />
<script type="text/javascript" src="/sview/script/jquery.js"></script>
<script type="text/javascript" src="/sview/script/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/sview/script/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="/sview/script/global.js"></script>
<script type="text/javascript" src="/sview/script/relations/dataset_edit.js"></script>
</head>
<body>
<%
		String lang = request.getParameter("lang");
		if (lang != null && lang.equals("zh")) {
			request.getRequestDispatcher("dataset_edit_zh.jsp").forward(request, response);
			return;
		}else if(lang==null){
			lang="en" ;
		}
		
		String genre = request.getParameter("genre");
		String datasetIdStr =  request.getParameter("datasetId");
		String idStr = request.getParameter("idStr");
		int datasetId =-1;
		String ds_type=null;
		if(genre==null){
			String location = "../relations/index.jsp";
			if(lang.equals("zh"))
				location = location+"?lang="+lang;
			response.sendRedirect(location);
			return;
		}else{
//			datasetId = Integer.parseInt(datasetIdStr);
//			ds_type = genre.equals("c")?"custom":"uri";
			if(genre.equals("c")){
				ds_type = DatasetItem.CUSTOM_GENRE;
				datasetId = Integer.parseInt(datasetIdStr);
			}else if(genre.equals("u")){
				ds_type = DatasetItem.URI_GENRE;
				datasetId = Integer.parseInt(datasetIdStr);
			}else if(genre.equals("t")){
				ds_type = DatasetItem.TEMP_GENRE;
			}
		}
		
		int ugid = -1;
		String ugname = (String)session.getAttribute("groupName");
		if(ugname != null){
			ugid = Group.getGroupIdByName(ugname);
		}else{
			System.out.println("going...no gid!");
			String location = "../relations/index.jsp";
			if(lang.equals("zh"))
				location = location+"?lang="+lang;
			response.sendRedirect(location);
			return;
		}
		
		String name = DatasetItem.getShowNameByDatasetID(datasetId, ds_type, lang);
      	String gname = null;
      	int gid = -1;
		if (genre.equals("c")){
//			name = CustomDataset.getNameByCustomDatasetID(datasetId);
			gname = (String)CustomDataset.getGroupByCustomDatasetId(datasetId);
			gid = Group.getGroupIdByName(gname);
//      		System.out.println(name+"|"+gname+"|"+gid);
//		} else if (genre.equals("u")) {
//			try {
//				name = URIResource.getLabel(datasetId, lang);
//			} catch (Throwable e) {
//				e.printStackTrace();
//			}
		}
      	System.out.println("genre:"+genre+"\tds_type:"+ds_type+"\tgid"+gid+"\tugid:"+ugid);
		
%>
<jsp:include page="/vheader.jsp" flush="true">
   <jsp:param name="f" value="d"/>
</jsp:include>
<%-- <% boolean existFlag = false; %>
<% 	
//对于原始uri named entity，查看所在编辑组是否已经定制过实体，并提示转到那个实体的编辑页面中
	if(request.getParameter("direct")==null&&genre.equals(Entity.URI_ENTITY_Str)){
		int custEntityID = EntityManagement.getInstance()
			.isCreated(etype,eid,gid,null);
		if(custEntityID>0){
			existFlag = true;
		}
		if(existFlag){
			String custEntityName = Entity.getNameByEntityID(custEntityID);
%>
   <div class="changeBox"> 
     <div class="warn"></div>
     <div class="text">
         <div class="d1">Your editing group has created the custom entity <span class="ename" eid="<%=custEntityID%>"><%=custEntityName%></span> on this uri.</div>
         <div class="d2"> Go to edit that custom enity now?</div>
         <div class="op"><input type="button" class="yes" value="Yes" /> <input type="button" class="no" value="No" /></div>
     </div>
   </div>
<%			
		}
	}
%>
<%if(!existFlag){ %> --%>
<div class="container">
   <div class="top">
      <div class="mspacing"></div>
      <%if(genre.equals("c")){ %>
      <div class="operation">Edit Custom Dataset</div>
      <%}else{ %>
       <div class="operation">Create Custom Dataset</div>
      <%} %>
      <div class="edit-save"> 
         <input type="button" value="Save" class="sbutton fb">
         <input type="button" value="Cancel" class="cbutton fb">
       </div>
   </div>
   <div class="mspacingLittle"></div>
      <div class="main">
	     <div class="entityName section">
	     	<div class="tab-header">
	           <div class="tab" >
			       <span class="title">Basic Information</span>
			       <span class="tab-status"></span>
			    </div>
	        </div>
			<div class="content">
			    <div class='namebox'>
                   <span class="label">Name:</span>
                   <span>
                      <%if(name==null){ %>
                      <input class="ftext inename" value="" type="text">
                      <%}else{ %>
                      <input class="ftext inename" value="<%=name%>" type="text">
                      <%} %>
                   </span>
                 </div>
               
                 <div class='descbox'>
                    <span class="label">Description:</span>
                    <%
						String comment = null;
//						ArrayList<DatasetComment> commentArray = null;
						if(ds_type.contentEquals(DatasetItem.URI_GENRE)){
							comment = DatasetComment.getCustomDescription(datasetId, lang);//.getOriginDescription(datasetId, lang);//.getOriginComment(datasetId, lang);
						}else if(ds_type.contentEquals(DatasetItem.CUSTOM_GENRE)){
							comment = DatasetComment.getCustomDescription(datasetId, lang);
//							commentArray = DatasetComment.getCommentsByDatasetLangUserOrderByTime(ds_type, datasetId, lang, null, 0, 1);
//							if(commentArray.size()>0){//only show first comment
//								DatasetComment dc = commentArray.get(0);
//								comment = dc.getText();
//							}//end of if_size
						}//end of if_custom
					%>
                    <span>
                    <%if(comment!=null&&!comment.trim().equals("")){ %>
                    	<textarea class="ftext commentarea "><%=comment %></textarea>
                    <%}else{ %>
                     	<textarea class="ftext commentarea "></textarea>
                    <%} %>
                    </span>
                </div>
                <div class="clear"></div>
             </div>
		 </div>
	     <div class="identity section">
		    <div class="tab-header">
	           <div class="tab" >
			       <span class="title">Sources </span>
			        <span class="tab-status"></span>
			    </div>
	        </div>
			<div class="content">
				<div class="leftPanel">
					<div class="entity-list">
					</div>
					<div class="advancedBox">
					    <a  href="javascript:void(0)" class="advancedSwitch">Add URI manually</a>
					    <div class="content">
					    	<div class='header'>Enter a uri to use as member:</div>
					    	<div><input id="uriAdded" type="text" size="75" value="http://"  class ="ftext"></div>
					    	<div class="opBox">
					    	<input type="button" value="Ok" class="addbtn" id="addbtn" />
					    	<input type="button" value="Reset" class="rstbtn" id="rstbtn" />
					    	</div>
					       <div class="input-status">
					    	  <div class="wait sts">
					    		  <div class="hint"><span>Checking...</span></div>
					    	  </div>
					    	  <div class="valid sts"></div>
					    	  <div class="invalid sts"><img src="/sview/icons/warning.png"></img>Sorry, SView did not find RDF description about the uri.</div>
					       </div>					    	
					    </div>
					</div>
				</div>
				<div class='rightPanel'>
				   <div class="recommendSection">
			        <ul>
		               <li><a href="#tabs-innersearch">Search</a></li>
		               <li><a href="#tabs-removed">Removed</a></li>
	                </ul>
                    <div id="tabs-innersearch">
                        <div class="innersearchBox">
                            <input id="query" type="text" size="75" value=""  class ="ftext" name="query">
         	                <input type="button" value="Search" class="sbtn" id="isearchq">
                        </div>                    
	                  	<div class="content">
	                  	   <ul class="entityList"></ul>
	                  	</div>
	                 </div>	                 
	                <div id="tabs-removed">
	                  	<div class="content">
	                  	   <ul class="entityList"></ul>
	                  	</div>
	                 </div>
				    </div>
				   </div>
			</div>
		 </div>	 
	   </div>
</div>
<div class="foot"></div>
<input type="hidden" id="ugid" value="<%=ugid %>" />
<input type="hidden" id="gid" value="<%=gid %>" />
<input type="hidden" id="ugname" value="<%=ugname %>" />
<input type="hidden" id="gname" value="<%=gname %>" />
<!-- <div class="hidden">
	<div id="image-op" title="Submit Image URI" >
    	<div class="title">Please input the image URI</div>
    	<div class="inputContainer">
      	<input id="imgURI" class="type-file-text" type="text">
   		</div>
    	<div class="img_hint">Prompt: please make sure the URI is valid</div>
    	<div class="status"><img id="loading"  src="/sview/icons/loading.gif"></div>
    	<div class="error"></div>
   		<div class="upload-btn">
      		<a  href="javascript:void(0)">
        		<span class="button-bg">
         	 		<span class="upload">Submit</span>
         		</span>
      		</a>
    	</div>
	</div>
</div>	 -->
<%-- <%if(name!=null){ %>
<input type="hidden" id="ename" value="<%=name%>" />
<%} %> --%>
<!-- <div class="warning">
	<div class="warnBox">
	    <p>Your editing group has created the custom dataset <span class="ename"></span> on this uri.</p>
	    <p>This uri cannot be member of the current custom entity under SView constraint.</p> 
	    <p>Go to edit <span class="ename"></span> now? </p> 
	</div>
</div> -->
<input type="hidden" id="display-lang" value="en" />
<input type="hidden" id="genre" value="<%=genre %>" />
<%if(genre.equals("c")){ %>
<input type="hidden" id="custom_type" value="edit" />
<%}else {%>
<input type="hidden" id="custom_type" value="create" />
<%} %>
<input type="hidden" id="ds_type" value="<%=ds_type %>" />
<input type="hidden" id="ds_id" value="<%=datasetId %>" />
<input type="hidden" id="idStr" value="<%=idStr %>" />
<jsp:include page="/vfooter.jsp"></jsp:include>
</body>
</html>